package com.example.softwareconfigurationcoursedesign;

import jakarta.servlet.ServletContext;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

@WebServlet(name = "Servlet", value = "/Servlet")
public class Servlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        String phoneNum = request.getParameter("phoneNumber2");
        String userPassword = request.getParameter("password2");
        response.setContentType("text/html;charset=utf-8");
        ServletContext context = this.getServletContext();
        String dbUrl = context.getInitParameter("DatabaseUrl");
        String dbUsername = context.getInitParameter("DatabaseUsername");
        String dbPassword = context.getInitParameter("DatabasePassword");
        String jdbcDriver = context.getInitParameter("jdbcDriver");
        System.err.println(phoneNum);
        System.err.println(userPassword);
        if (phoneNum.equals("admin") && (userPassword.equals("admin"))) {
            response.sendRedirect("wait.html");
        } else {
            try (Connection connection = DatabaseUtil.getCon(jdbcDriver, dbUrl, dbUsername, dbPassword); PreparedStatement pst = connection.prepareStatement("select * from users where phone_number=?")) {
                pst.setString(1, phoneNum);
                try (ResultSet rs = pst.executeQuery()) {
                    if (rs.next()) {
                        String Password = rs.getString("password");
                        if (userPassword.equals(Password)) {
                            log("登录");
                            response.sendRedirect("TransitionInterface.html");
//                        response.sendRedirect("wait.html");
                        } else {
                            PrintWriter out = response.getWriter();
                            out.println("<script>alert('登录失败 将返回主页面  请您再次尝试进行登录'); window.location.href='index.jsp';</script>");
                            out.flush();
                        }
                    } else {
                        PrintWriter out = response.getWriter();
                        out.println("<script>alert('登录失败 将返回主页面  请您再次尝试进行登录'); window.location.href='index.jsp';</script>");
                        out.flush();
                    }
                }
            } catch (SQLException | IOException throwable) {
                Logger.getLogger(Servlet.class.getName()).log(Level.SEVERE, "数据库操作失败", throwable);
                response.sendRedirect("error.html");
            }
        }
    }
}
